/*
 * Copyright (C) 2023 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.xuexiang.templateproject.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
//import android.widget.ImageView;
//import android.widget.LinearLayout;
//import android.widget.TextView;
//import android.widget.Toast;
//
//import androidx.annotation.NonNull;
//import androidx.core.util.Pair;

import androidx.annotation.NonNull;
import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;
import com.xuexiang.templateproject.bean.Box;
import com.xuexiang.templateproject.bean.ImageBoxEntity;
import com.xuexiang.templateproject.bean.LeftBottom;
import com.xuexiang.templateproject.bean.LeftTop;
import com.xuexiang.templateproject.bean.PageCourse;
import com.xuexiang.templateproject.bean.RightBottom;
import com.xuexiang.templateproject.bean.RightTop;
import com.xuexiang.templateproject.bean.SubjectManagerEntity;
import com.xuexiang.templateproject.core.BaseActivity;
import com.xuexiang.templateproject.databinding.ActivityImageBoxBinding;
//import com.xuexiang.templateproject.databinding.ActivityPictureCropBinding;
import com.xuexiang.templateproject.presenter.ImageBoxPresenter;
import com.xuexiang.templateproject.utils.Constants;
import com.xuexiang.templateproject.utils.DialogManager;
import com.xuexiang.templateproject.utils.Sp;
//import com.xuexiang.templateproject.utils.TokenUtils;
import com.xuexiang.templateproject.view.ImageBoxContract;
import com.xuexiang.templateproject.widget.BoxView;
import com.xuexiang.templateproject.widget.ImageBoxView;
import com.xuexiang.templateproject.widget.SubjectDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;
import kotlin.TuplesKt;
import kotlin.collections.MapsKt;
import kotlin.jvm.internal.Intrinsics;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import com.xuexiang.templateproject.R;
import com.xuexiang.templateproject.widget.showLoadingDialog;
import com.xuexiang.xutil.app.ActivityUtils;
import com.xuexiang.xutil.file.FileUtils;
import com.xuexiang.xutil.tip.ToastUtils;
//import org.jetbrains.anko.internals.AnkoInternals;

public final class ImageBoxActivity extends BaseActivity<ActivityImageBoxBinding> implements ImageBoxContract {
    @Nullable

    /* renamed from: b  reason: collision with root package name */
    public ArrayList<ImageBoxEntity> mImageBoxList;
    @Nullable

    /* renamed from: c  reason: collision with root package name */
    public String f3959c;
    @Nullable

    /* renamed from: d  reason: collision with root package name */
    public BoxView mBoxView;

    /* renamed from: e  reason: collision with root package na me */
    public int mTempHeight;
    @Nullable

    /* renamed from: f  reason: collision with root package name */
    public ImageBoxEntity mImageBoxEntity;

    /* renamed from: g  reason: collision with root package name */
    public int mIndex;
    @Nullable

    /* renamed from: i  reason: collision with root package name */
    public SubjectDialog subjectDialog;

    /* renamed from: k  reason: collision with root package name */
    public TabLayout.OnTabSelectedListener onTabSelectListener;
    @NotNull

    /* renamed from: l  reason: collision with root package name */
    public Map<Integer, View> f3968l = new LinkedHashMap();
    @NotNull

    /* renamed from: h  reason: collision with root package name */
    public String f3964h = "";
    private  ImageBoxPresenter mImageBoxPresenter=null;
//    @NotNull

    /* renamed from: j  reason: collision with root package name */
//    public final Lazy f3966j = LazyKt.lazy(ImageBoxActivity$mPresenter$2.f3977a);
    @Override
    protected ActivityImageBoxBinding viewBindingInflate(LayoutInflater inflater) {
        return ActivityImageBoxBinding.inflate(inflater);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        initView();
        initData();
        initEvent();
    }
    @Override
    public void bizError(@NonNull String str, int i2) {

    }

    @Override
    public void dismissLoading() {
        showLoadingDialog.hideLoading();
    }

    @NonNull
    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public void showLoading() {
        showLoadingDialog.showLoading(this);
    }

    public final class a implements SubjectDialog.ItemOnClickListener {
        public a() {
        }

        @Override // com.jcds.learneasy.dialog.SubjectDialog.a
        public void itemOnClickListener(@NotNull PageCourse pageCourse) {
//            Intrinsics.checkNotNullParameter(pageCourse, "pageCourse");
            RequestBody create = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), new Gson().toJson(MapsKt.mapOf(TuplesKt.to(Constants.Bundle.TRANSACTIONID, ImageBoxActivity.this.f3959c), TuplesKt.to("rects", ((ImageBoxView) ImageBoxActivity.this.binding.ibView).getImageBox()), TuplesKt.to("courseId", Integer.valueOf(pageCourse.getCourseId())))));
//            Intrinsics.checkNotNullExpressionValue(create, "create(\n                …      Gson().toJson(map))");
            ImageBoxActivity.this.getMPresenter().getSimpleSubmit(create);
        }

    }

    public final class b implements DialogManager.OnClickListener {
        public b() {
        }

        @Override // d.m.b.g.n0
        public void a() {
//            m.b.a.c.c().k(new d.m.b.h.a(1004));
//            AnkoInternals.internalStartActivity(ImageBoxActivity.this, MainActivity.class, new Pair[0]);

            ActivityUtils.startActivity(BoxEditActivity.class);
        }

        @Override // d.m.b.g.n0
        public void b() {
            ImageBoxActivity.this.finish();
        }
    }

    public final class c implements ViewTreeObserver.OnGlobalLayoutListener {
        public c() {
        }

        @Override // android.view.ViewTreeObserver.OnGlobalLayoutListener
        public void onGlobalLayout() {
            ImageBoxActivity imageBoxActivity = ImageBoxActivity.this;
            int i2 = R.id.ibView;
            ((ImageBoxView) ImageBoxActivity.this.binding.ibView).getViewTreeObserver().removeOnGlobalLayoutListener(this);
            ArrayList arrayList = ImageBoxActivity.this.mImageBoxList;
            Intrinsics.checkNotNull(arrayList);
            ((ImageBoxView) ImageBoxActivity.this.binding.ibView).m(arrayList, ImageBoxActivity.this);
            StringBuilder sb = new StringBuilder();
            sb.append("选错题(1/");
            ArrayList arrayList2 = ImageBoxActivity.this.mImageBoxList;
            Intrinsics.checkNotNull(arrayList2);
            sb.append(arrayList2.size());
            sb.append(')');
            ImageBoxActivity.this.binding.tvTitle.setText(sb.toString());
        }
    }

    public final class d implements ImageBoxView.OnBoxEditClickListener {
        public d() {
        }

        @Override // com.jcds.learneasy.widget.ImageBoxView.b
        public void onEditClick(@NotNull ImageBoxEntity imageBoxEntity, @NotNull BoxView boxView) {
//            Intrinsics.checkNotNullParameter(imageBoxEntity, "imageBoxEntity");
//            Intrinsics.checkNotNullParameter(boxView, "boxView");
            ImageBoxActivity.this.mBoxView = boxView;
            Box box = boxView.getBox();
            ImageBoxActivity.this.mImageBoxEntity = imageBoxEntity;
//            TrackEventUtil.onTrack(ImageBoxActivity.this, "image_wrong");
//            AnkoInternals.internalStartActivityForResult(ImageBoxActivity.this, BoxEditActivity.class, 1, new Pair[]{new Pair("ImageBoxEntity", imageBoxEntity), new Pair("box", box)});
            Map<String,Object> map = new HashMap<>();
            map.put("ImageBoxEntity", imageBoxEntity);
            map.put("box", box);
            ActivityUtils.startActivity(BoxEditActivity.class,map);
        }
    }


    public final class e implements ImageBoxView.ObservableListener {
        public e() {
        }

        @Override // com.jcds.learneasy.widget.ImageBoxView.a
        public void a(int i2) {
            String str;
            if (i2 > 0) {
                StringBuilder sb = new StringBuilder();
                sb.append('(');
                sb.append(i2);
                sb.append(')');
                str = sb.toString();
            } else {
                str = "";
            }

            binding.tvConfirm.setText(ImageBoxActivity.this.getString(R.string.match_wrong_box_save, new Object[]{str}));
        }

        @Override // com.jcds.learneasy.widget.ImageBoxView.a
        public void b(int i2, int i3) {
            binding.tvTitle.setText("选错题(" + (i2 + 1) + '/' + i3 + ')');
        }
    }

    public final class f implements TabLayout.OnTabSelectedListener {


        public final class a implements DialogManager.OnClickListener {

            /* renamed from: a  reason: collision with root package name */
            public final /* synthetic */ ImageBoxActivity f3975a;

            public a(ImageBoxActivity imageBoxActivity) {
                this.f3975a = imageBoxActivity;
            }

            @Override // d.m.b.g.n0
            public void a() {
                ImageBoxActivity imageBoxActivity = this.f3975a;
                TabLayout tabLayout = ImageBoxActivity.this.binding.tabLayout;
                TabLayout.OnTabSelectedListener onTabSelectedListener = this.f3975a.onTabSelectListener;
                TabLayout.OnTabSelectedListener onTabSelectedListener2 = null;
                if (onTabSelectedListener == null) {
                    Intrinsics.throwUninitializedPropertyAccessException("onTabSelectListener");
                    onTabSelectedListener = null;
                }
                tabLayout.removeOnTabSelectedListener(onTabSelectedListener);
                TabLayout.Tab tabAt = tabLayout.getTabAt(1);
                if (tabAt != null) {
                    tabAt.select();
                }
                TabLayout.OnTabSelectedListener onTabSelectedListener3 = this.f3975a.onTabSelectListener;
                if (onTabSelectedListener3 == null) {
                    Intrinsics.throwUninitializedPropertyAccessException("onTabSelectListener");
                } else {
                    onTabSelectedListener2 = onTabSelectedListener3;
                }
                tabLayout.addOnTabSelectedListener(onTabSelectedListener2);
            }

            @Override // d.m.b.g.n0
            public void b() {
//                TrackEventUtil.onTrack(this.f3975a, "match_wrong");
                this.f3975a.getMPresenter().getSaveSchema("default");
                this.f3975a.f3964h = "default";
            }
        }


        public  final class b implements DialogManager.OnClickListener {

            /* renamed from: a  reason: collision with root package name */
            public final /* synthetic */ ImageBoxActivity f3976a;

            public b(ImageBoxActivity imageBoxActivity) {
                this.f3976a = imageBoxActivity;
            }

            @Override // d.m.b.g.n0
            public void a() {
                ImageBoxActivity imageBoxActivity = this.f3976a;
                int i2 = R.id.tabLayout;
                TabLayout tabLayout = (TabLayout) imageBoxActivity.binding.tabLayout;
                TabLayout.OnTabSelectedListener onTabSelectedListener = this.f3976a.onTabSelectListener;
                TabLayout.OnTabSelectedListener onTabSelectedListener2 = null;
                if (onTabSelectedListener == null) {
                    Intrinsics.throwUninitializedPropertyAccessException("onTabSelectListener");
                    onTabSelectedListener = null;
                }
                tabLayout.removeOnTabSelectedListener(onTabSelectedListener);
                TabLayout.Tab tabAt = ((TabLayout) this.f3976a.binding.tabLayout).getTabAt(0);
                if (tabAt != null) {
                    tabAt.select();
                }
                TabLayout tabLayout2 = (TabLayout) this.f3976a.binding.tabLayout;
                TabLayout.OnTabSelectedListener onTabSelectedListener3 = this.f3976a.onTabSelectListener;
                if (onTabSelectedListener3 == null) {
                    Intrinsics.throwUninitializedPropertyAccessException("onTabSelectListener");
                } else {
                    onTabSelectedListener2 = onTabSelectedListener3;
                }
                tabLayout2.addOnTabSelectedListener(onTabSelectedListener2);
            }

            @Override // d.m.b.g.n0
            public void b() {
//                TrackEventUtil.onTrack(this.f3976a, "minimalism_wrong");
                this.f3976a.getMPresenter().getSaveSchema("simple");
                this.f3976a.f3964h = "simple";
            }
        }

        public f() {
        }

        @Override // com.google.android.material.tabs.TabLayout.BaseOnTabSelectedListener
        public void onTabReselected(@Nullable TabLayout.Tab tab) {
        }

        @Override // com.google.android.material.tabs.TabLayout.BaseOnTabSelectedListener
        public void onTabSelected(@Nullable TabLayout.Tab tab) {
            Object tag = tab != null ? tab.getTag() : null;
            if (Intrinsics.areEqual(tag, (Object) 0)) {
                ImageBoxActivity imageBoxActivity = ImageBoxActivity.this;
                DialogManager.showModeSelect(imageBoxActivity, "确定要切换到匹配模式?", "切换后将有原题匹配，答案解析", "取消", "确认切换", null, new a(imageBoxActivity));
            } else if (Intrinsics.areEqual(tag, (Object) 1)) {
                ImageBoxActivity imageBoxActivity2 = ImageBoxActivity.this;
                DialogManager.showModeSelect(imageBoxActivity2, "确定要切换到极简模式?", "切换后错题将批量保存为去手写图", "取消", "确认切换", null, new b(imageBoxActivity2));
            }
        }

        @Override // com.google.android.material.tabs.TabLayout.BaseOnTabSelectedListener
        public void onTabUnselected(@Nullable TabLayout.Tab tab) {
        }
    }

    public ImageBoxActivity() {
        getMPresenter().attachView(this);
    }

    public static final void S0(ImageBoxActivity this$0, View view) {
        Intrinsics.checkNotNullParameter(this$0, "this$0");
        ArrayList<Box> imageBox =this$0.binding.ibView.getImageBox();
        Intent intent = new Intent();
        if (imageBox.isEmpty()) {
            String string = this$0.getString(R.string.match_border_first);
            Intrinsics.checkNotNullExpressionValue(string, "getString(R.string.match_border_first)");
//            ExtensionsKt.showToast(this$0, string);
            ToastUtils.toast(string);
            return;
        }
        ArrayList<ImageBoxEntity> arrayList = this$0.mImageBoxList;
        if (arrayList != null) {
            for (ImageBoxEntity imageBoxEntity : arrayList) {
                if (!imageBoxEntity.getNeedDownload()) {
                    FileUtils.deleteFile(imageBoxEntity.getFilePath());
                }
            }
        }
        if (!Sp.INSTANCE.getSchema_show()) {
            this$0.getMPresenter().getSubjects();
        } else if (Intrinsics.areEqual(this$0.f3964h, "simple")) {
            this$0.getMPresenter().getSubjects();
        } else {
//            intent.putParcelableArrayListExtra("list", imageBox);
//            intent.putExtra(Constants.Bundle.TRANSACTIONID, this$0.f3959c);
//            intent.setClass(this$0, NewMatchActivity.class);
//            this$0.startActivity(intent);
//            this$0.finish();
        }
    }

    public static final void T0(ImageBoxActivity this$0, View view) {
        Intrinsics.checkNotNullParameter(this$0, "this$0");

        int mScrollY = this$0.binding.ibView.getMScrollY();
        int mWidth = this$0.binding.ibView.getMWidth();
        ArrayList<ImageBoxEntity> arrayList = this$0.mImageBoxList;
        Intrinsics.checkNotNull(arrayList);
        int size = arrayList.size();
        int i3 = 0;
        int i4 = 0;
        while (true) {
            if (i3 >= size) {
                i3 = 0;
                break;
            }
            ArrayList<ImageBoxEntity> arrayList2 = this$0.mImageBoxList;
            Intrinsics.checkNotNull(arrayList2);
            int width = arrayList2.get(i3).getWidth();
            ArrayList<ImageBoxEntity> arrayList3 = this$0.mImageBoxList;
            Intrinsics.checkNotNull(arrayList3);
            int height = (int) ((mWidth / width) * arrayList3.get(i3).getHeight());
            i4 += height;
            if (mScrollY >= i4) {
                i3++;
            } else if (i4 - mScrollY > height / 2) {
                this$0.mTempHeight = i4 - height;
            } else {
                i3++;
                this$0.mTempHeight = i4;
            }
        }
        this$0.mIndex = i3;
        ArrayList<ImageBoxEntity> arrayList4 = this$0.mImageBoxList;
        Intrinsics.checkNotNull(arrayList4);
        ImageBoxEntity imageBoxEntity = arrayList4.get(i3);
        Intrinsics.checkNotNullExpressionValue(imageBoxEntity, "imageList!![index]");
        String uuid = UUID.randomUUID().toString();
        Intrinsics.checkNotNullExpressionValue(uuid, "randomUUID().toString()");
        int i5 = i3 + 1;

        int boxCount =this$0.binding.ibView.getBoxCount();
        this$0.binding.ibView.setBoxCount(boxCount + 1);
//        AnkoInternals.internalStartActivityForResult(this$0, BoxEditActivity.class, 2, new Pair[]{new Pair("ImageBoxEntity", imageBoxEntity), new Pair("box", new Box(i5, boxCount, new LeftBottom(0.05f, 0.7f), new LeftTop(0.05f, 0.3f), new RightBottom(0.95f, 0.7f), new RightTop(0.95f, 0.3f), false, uuid))});
        Map<String,Object> map = new HashMap<>();
        map.put("ImageBoxEntity", imageBoxEntity);
        map.put("box", new Box(i5, boxCount, new LeftBottom(0.05f, 0.7f), new LeftTop(0.05f, 0.3f), new RightBottom(0.95f, 0.7f), new RightTop(0.95f, 0.3f), false, uuid));
        ActivityUtils.startActivity(BoxEditActivity.class,map);
    }

    public static final void U0(ImageBoxActivity this$0, View view) {
        Intrinsics.checkNotNullParameter(this$0, "this$0");
        this$0.finish();
    }

    public final ImageBoxPresenter getMPresenter() {
        if(mImageBoxPresenter==null){
            mImageBoxPresenter = new ImageBoxPresenter();
        }
        return mImageBoxPresenter;
    }


    @Override // d.m.b.f.j
    public void getSubjectSuccess(@NotNull SubjectManagerEntity subject) {
        SubjectDialog subjectDialog;
        Intrinsics.checkNotNullParameter(subject, "subject");
        if (this.subjectDialog == null) {
            this.subjectDialog = new SubjectDialog(this, subject.getHomepageCourseList(), new a());
        }
        SubjectDialog subjectDialog2 = this.subjectDialog;
        Intrinsics.checkNotNull(subjectDialog2);
        if (!subjectDialog2.isShowing() && !isFinishing() && (subjectDialog = this.subjectDialog) != null) {
            subjectDialog.show();
        }
    }


    public void initData() {

        this.mImageBoxList = getIntent().getParcelableArrayListExtra(Constants.Bundle.IMAGE_LIST);
        this.f3959c = getIntent().getStringExtra(Constants.Bundle.TRANSACTIONID);
    }


    public void initEvent() {

        binding.ibView.getViewTreeObserver().addOnGlobalLayoutListener(new c());
        binding.tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ImageBoxActivity.S0(ImageBoxActivity.this, view);
            }
        });
        binding.ibView.setOnEditClickListener(new d());
        binding.ibView.setObservableListener(new e());

        binding.llAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ImageBoxActivity.T0(ImageBoxActivity.this, view);
            }
        });
        binding.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ImageBoxActivity.U0(ImageBoxActivity.this, view);
            }
        });


    }


    public void initView() {
        binding.tvConfirm.setText(getString(R.string.match_wrong_box_save, ""));

        Sp sp = Sp.INSTANCE;
        this.f3964h = sp.getSchema();
        if (sp.getSchema_show()) {
            this.onTabSelectListener = new f();
            binding.tabLayout.setVisibility(View.VISIBLE);
            String[] stringArray = getResources().getStringArray(R.array.schema_tabs);

            int length = stringArray.length;
            for (int i2 = 0; i2 < length; i2++) {
                TabLayout.Tab newTab = binding.tabLayout.newTab();
                newTab.setTag(Integer.valueOf(i2));
                binding.tabLayout.addTab(newTab.setText(stringArray[i2]));
            }
            if (Intrinsics.areEqual(Sp.INSTANCE.getSchema(), "simple")) {

                ((TabLayout) binding.tabLayout).selectTab(((TabLayout) binding.tabLayout).getTabAt(1));
            } else {

                ((TabLayout) binding.tabLayout).selectTab(((TabLayout) binding.tabLayout).getTabAt(0));
            }

            TabLayout.OnTabSelectedListener onTabSelectedListener = this.onTabSelectListener;
            binding.tabLayout.addOnTabSelectedListener(onTabSelectedListener);
            return;
        }
        binding.tabLayout.setVisibility(View.GONE);
    }

    @Override // d.m.b.f.j
    public void getSaveSchemaSuccess() {
        Sp.INSTANCE.setSchema(this.f3964h);
    }

    @Override // d.m.b.f.j
    public void getSubmitSuccess() {
        String string = getString(R.string.match_save_dialog_num, new Object[]{Integer.valueOf( binding.ibView.getImageBox().size())});
        String string2 = getString(R.string.match_view_wrong);
        String string3 = getString(R.string.match_continue_wrong);
        DialogManager.r(this, string, string2, string3, R.mipmap.iv_src_match_complete, new b());
    }

    @Override // com.jcds.learneasy.base.LnBaseActivity, androidx.fragment.app.FragmentActivity, androidx.activity.ComponentActivity, android.app.Activity
    public void onActivityResult(int i2, int i3, @Nullable Intent intent) {
        Bundle extras;
        Bundle extras2;
        super.onActivityResult(i2, i3, intent);
        if (i3 == 1) {
            Object obj = null;
            if (i2 == 1) {
                if (!(intent == null || (extras = intent.getExtras()) == null)) {
                    obj = extras.get("box");
                }
                Box box = (Box) obj;
                BoxView boxView = this.mBoxView;
                if (boxView != null) {
                    boxView.setBox(box);
                }
                binding.ibView.s(this.mBoxView, this.mImageBoxEntity);
            } else if (i2 == 2) {
                if (!(intent == null || (extras2 = intent.getExtras()) == null)) {
                    obj = extras2.get("box");
                }
                ImageBoxEntity imageBoxEntity2 = this.mImageBoxList.get(this.mIndex);
                binding.ibView.j((Box) obj, imageBoxEntity2, this.mTempHeight);
            }
        } else if (i3 == 2 && i2 == 1) {
            binding.ibView.o(mBoxView);
        }
    }
}